//
//  GoodsListTab.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/15.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "GoodsListTab.h"
#import "GoodsListCell.h"
#import "ProductModel.h"
#import "PdcDetailVC.h"
#import "NoDataView.h"
//所有商品URL
#define GOODSLIST_URL @"/api/ApiGoods/getGoodsList"

@interface GoodsListTab ()
{
    //当前页数
    int _currentPage;
    //数据源
    NSMutableArray *_data;
}
@end
@implementation GoodsListTab

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.dataSource = self;
        
        self.delegate = self;
        self.dataSource = self;
        _currentPage = 1;
        _data = [NSMutableArray array];
        [self registerNib:[UINib nibWithNibName:@"GoodsListCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        self.rowHeight = 105.0f;
        self.tableFooterView = [[UIView alloc]init];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 2.集成刷新控件
        [self setupRefresh];
    }
    return self;
}
/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    //    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    // dateKey用于存储刷新时间，可以保证不同界面拥有不同的刷新时间
    [self addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
    //自动刷新(一进入程序就下拉刷新)
    [self headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self addFooterWithTarget:self action:@selector(footerRereshing)];
}
#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    _currentPage = 1;
    [_data removeAllObjects];
    [self startRequestForData];
}

- (void)footerRereshing
{
    _currentPage++;
    [self startRequestForData];
}

//改写set方法
- (void)setType_flag:(NSString *)type_flag
{
    _type_flag = type_flag;
    [self startRequestForData];
}
//请求网络数据
- (void)startRequestForData
{
    NSDictionary *params = @{@"order":self.type_flag,@"filter":@"20",@"num":@"20",@"page":[NSNumber numberWithInt:_currentPage]};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,GOODSLIST_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        if ([ret intValue] == 0) {
            if (_currentPage == 1 && _data.count>0) {
                return ;
            }
            NSDictionary *ddc = dic[@"data"];
            NSArray *pdcs = ddc[@"list"];
            for (NSDictionary *pdc in pdcs) {
                ProductModel *model = [[ProductModel alloc]init];
                model.pdc_id = pdc[@"id"];
                model.pdc_sid = pdc[@"sid"];
                model.pdc_qishu = pdc[@"qishu"];
                model.pdc_title = pdc[@"title"];
                model.pdc_sub_title = pdc[@"title2"];
                model.pdc_desc = pdc[@"description"];
                model.pdc_people_indeed = pdc[@"zongrenshu"];
                model.pdc_people_now = pdc[@"canyurenshu"];
                model.pdc_people_left = pdc[@"shenyurenshu"];
                model.pdc_img_path = pdc[@"thumb_url"];
                [_data addObject:model];
            }
            [self reloadData];
            if(_data.count > 0){
                for (UIView *v in self.subviews) {
                    if (v.tag == 12121) {
                        [v removeFromSuperview];
                    }
                }
            } 
        }else{
            if(_data.count == 0){
                [self addNODataView];
            }
        }
        [self headerEndRefreshing];
        [self footerEndRefreshing];
        } failedBlock:^{
        if(_data.count > 0){
            for (UIView *v in self.subviews) {
                if (v.tag == 12121) {
                    [v removeFromSuperview];
                }
            }
        }else{
            [self addNODataView];
        }
        [self headerEndRefreshing];
        [self footerEndRefreshing];
    } needLog:NO];
}
//没有数据默认填充的控件
- (void)addNODataView{
    NoDataView *nodateView = [[[NSBundle mainBundle]loadNibNamed:@"NoDataView" owner:self options:nil]lastObject];
    nodateView.tag = 12121;
    nodateView.frame = CGRectMake(0, 0, WYMWITH, self.contentSize.height);
    nodateView.isMineFlag = NO;
    nodateView.title = @"获取数据失败!";
    [self addSubview:nodateView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (_data.count>0) {
        //获取当前数据模型
        ProductModel *model = _data[indexPath.row];
        //填充cell
        [cell fillCellWithModel:model];
        cell.tableView = self;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //保存至兴趣记录
    ProductModel *currentModel = _data[indexPath.row];
    WYShopCarModel *model = [[WYShopCarModel alloc]init];
    model.car_id = currentModel.pdc_id;
    model.car_new_id = currentModel.pdc_sid;
    model.name = currentModel.pdc_title;
    model.imgPath = currentModel.pdc_img_path;
    model.count = 1;
    [[WYDataBase sharedDataBase]insertPdcToIntrestCarWithModel:model];
    
    
    //跳往商品详情
    PdcDetailVC *detailVC = [[PdcDetailVC alloc]init];
    detailVC.pdc_id = currentModel.pdc_id;
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:detailVC animated:YES];
    self.father.isFirstIn = NO;
}
@end
